package com.silence.whereeat.dao;

import com.silence.whereeat.model.FoodInfo;
import com.silence.whereeat.service.FoodInfoService;
import com.silence.whereeat.utils.DBUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 食品信息服务
 * Created by KIM on 2015/9/1.
 */
public class FoodInfoDao implements FoodInfoService {

    private DBUtils dbUtils;

    public FoodInfoDao() {
        dbUtils = new DBUtils();
    }

    public int addFoodInfo(FoodInfo foodInfo) {
        String sql = "INSERT INTO foodinfo (`food_name`,`food_price`,`food_introduction`,`store_id`) VALUES (?,?,?,?);";
        int result = -1;
        List<Object> list = new ArrayList<Object>();
        list.add(foodInfo.getFood_name());
        list.add(foodInfo.getFood_price());
        list.add(foodInfo.getFood_introduction());
        list.add(foodInfo.getStore_id());
        try {
            result = dbUtils.updateByPreParedStatement(sql, list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public int deleteFoodInfo(int foodID) {
        String sql = "DELETE FROM foodinfo WHERE food_id=?;";
        int result = -1;
        List<Object> list = new ArrayList<Object>();
        list.add(foodID);
        try {
            result = dbUtils.updateByPreParedStatement(sql, list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public int editFoodInfo(FoodInfo foodInfo) {
        String sql = "UPDATE foodinfo " +
                "SET food_name=?," +
                "food_price=?," +
                "food_image=?," +
                "food_introduction=?," +
                "WHERE food_id=?;";
        List<Object> list = new ArrayList<Object>();
        list.add(foodInfo.getFood_name());
        list.add(foodInfo.getFood_price());
        list.add(foodInfo.getFood_image());
        list.add(foodInfo.getFood_introduction());
        list.add(foodInfo.getFood_id());
        int result = -1;
        try {
            result = dbUtils.updateByPreParedStatement(sql, list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public FoodInfo getFoodInfoById(int foodID) {
        String sql = "SELECT * FROM foodinfo WHERE food_id=?;";
        List<Object> list = new ArrayList<Object>();
        list.add(foodID);
        FoodInfo foodInfo = null;
        try {
            foodInfo = dbUtils.querySingleResult(sql, list, FoodInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return foodInfo;
    }

    public List<FoodInfo> getFoodInfoByName(String name) {
        String sql = "SELECT * " +
                "FROM foodinfo " +
                "WHERE food_name LIKE ?;";
        String str = "%";
        for (int i = 0; i < name.length(); i++) {
            str += name.charAt(i) + "%";
        }
        List<Object> list = new ArrayList<Object>();
        list.add(str);
        List<FoodInfo> foodInfos = null;
        try {
            foodInfos = dbUtils.queryResultSet(sql, list, FoodInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return foodInfos;
    }

    public List<FoodInfo> getFoodInfos(int passage) {
        String sql = "SELECT * " +
                "FROM foodinfo " +
                "LIMIT ?,?;";
        List<Object> list = new ArrayList<Object>();
        list.add(passage * 10);
        list.add(passage * 10 + 9);
        List<FoodInfo> foodInfos = null;
        try {
            foodInfos = dbUtils.queryResultSet(sql, list, FoodInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return foodInfos;
    }

    public void close() {
        dbUtils.close();
    }
}
